package br.com.viaembratel.view.backing.vialivre.consumidor;


import br.com.viaembratel.vialivre.modelo.prepago.consumidor.v1_0_0.types.ConsumidorType;
import br.com.viaembratel.vialivre.modelo.prepago.contratacao.v1_0_0.types.ContratacaoType;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.faces.event.PhaseEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.view.rich.component.rich.RichDocument;
import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.output.RichMessages;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;


public class ConsultaHistoricoContratacoes {
    private RichForm f1;
    private RichDocument d1;
    private RichTable t1;
    private RichMessages m1;

    public void setF1(RichForm f1) {
        this.f1 = f1;
    }

    public RichForm getF1() {
        return f1;
    }

    public void setD1(RichDocument d1) {
        this.d1 = d1;
    }

    public RichDocument getD1() {
        return d1;
    }

    public void setT1(RichTable t1) {
        this.t1 = t1;
    }

    public RichTable getT1() {
        return t1;
    }

    public void setM1(RichMessages m1) {
        this.m1 = m1;
    }

    public RichMessages getM1() {
        return m1;
    }

    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void populaDados(PhaseEvent phaseEvent) {

        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("obterConsumidor");
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("erroConsulta",
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          "",
                                                                          "Ocorreu um erro durante a consulta do consumidor."));
        } else {

            RichTable richTable = this.getT1();
            if (result != null) {
                List<ContratacaoType> contratacoes =
                    ((ConsumidorType)result).getContratacoes().getContratacao();
                richTable.setValue(contratacoes);
                richTable.setRows(contratacoes.size());
                richTable.setFetchSize(contratacoes.size());
            }
        }
    }
}
